import GenerateNewID.DataCell;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

public class dataGenerator {
    public static String test_data ="src/data/test_data_56.txt";
    public static String result_data ="src/data/56_ans.txt";
    public static String test_data_28w ="src/data/official/test_data_28w.txt";
    public static String result_data_28 ="src/data/official/result_28w.txt";
    public static int fold = 200;
    public ArrayList<DataCell> loadData(){
        String fileName= test_data;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException exception) {
            System.err.println(fileName + " File Not Found");
        }
        String line = "";
        ArrayList<DataCell> ret = new ArrayList<>();
        try {
            while ((line = reader.readLine()) != null) {
                ret.add(new DataCell(line));
            }
        } catch (IOException exception) {
            System.err.println(exception.getMessage());
        }
        return ret;
    }
    public ArrayList<String> loadResultData(){
        String fileName = result_data;
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(fileName));
        } catch (FileNotFoundException exception) {
            System.err.println(fileName + " File Not Found");
        }
        String line = "";
        ArrayList<String> ret = new ArrayList<String>();
        try {
            while ((line = reader.readLine()) != null) {
                    ret.add(line.trim());
            }
        } catch (IOException exception) {
            System.err.println(exception.getMessage());
        }
        return ret;
    }
    public static void writeFile(String path,String content){
        FileWriter fileWritter = null;
        try {
            fileWritter = new FileWriter(path,false);
            fileWritter.write(content);
            fileWritter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static void main(String[] args) {
        dataGenerator  dg = new dataGenerator();
        ArrayList<DataCell>  initData = dg.loadData();
        ArrayList<String>    iniRet   = dg.loadResultData();
        iniRet.remove(0);           // 去掉答案总个数
        HashMap<Integer,Integer> new2original = new HashMap<>();
        int step = 7000; //5k的数据最大不超过7000
        ArrayList<DataCell>  generaeData = new ArrayList<>(initData);
        ArrayList<String>    generaeRet= new ArrayList<>(iniRet);
        //额外生成55份，共28w行数据
        for(int i =1;i<=fold;i++){
            // 生成1份test_data
            for(DataCell e: initData){
                Integer newIdFrom = e.IDFrom+step*i;
                Integer newIdTo =   e.IDTo + step*i;
                DataCell n = new DataCell(newIdFrom,newIdTo,e.amount);
                generaeData.add(n);
            }
            ArrayList<String> curRet = new ArrayList<>();
            // 生成1份result
            for(String line:iniRet){
                StringBuilder a= new StringBuilder();
               String[] split = line.split(",");
               for(String s:split){
                   a.append(Integer.valueOf(s)+step*i+",");
               }
               a.deleteCharAt(a.length()-1);
               curRet.add(a+"");
            }
            generaeRet.addAll(curRet);
        }
        // 对答案进行排序
        Collections.sort(generaeRet, new Comparator<String>() {
            public int compare(String o1, String o2) {
                String[] o1spit = o1.split(",");
                String[] o2spit = o2.split(",");
                if(o1spit.length!=o2spit.length) return o1spit.length-o2spit.length;
                for (int i = 0; i < o1spit.length; i++) {
                    if(o1spit[i].equals(o2spit[i])) continue;
                    return Integer.valueOf(o1spit[i])-Integer.valueOf(o2spit[i]);
                }return  0;
            }
        });
        // 生成写入的字符串
        StringBuilder  test_write = new StringBuilder();
        StringBuilder  ret_write =  new StringBuilder();
        ret_write.append(generaeRet.size()+"\n");       // 先写入答案总数
        for(DataCell e:generaeData){
            test_write.append(e.IDFrom+","+e.IDTo+","+e.amount+"\n");
        }
        for(String e:generaeRet){
            ret_write.append(e+"\n");
        }
        writeFile(test_data_28w,test_write.toString());
        writeFile(result_data_28,ret_write.toString());
        System.out.println("共生成 test_data  "+generaeData.size()+" 条数据");
        System.out.println("共生成 result   "+generaeRet.size()+" 条数据");
    }
}

//class GenerateNewID.DataCell{
//    public Integer IDFrom =0;
//    public Integer IDTo =0;
//    public Integer amount = 0;
//    public GenerateNewID.DataCell(String line) {
//        String[] row = line.split(",");
//        this.IDFrom = Integer.valueOf(row[0]);
//        this.IDTo = Integer.valueOf(row[1]);
//        this.amount =Integer.valueOf(row[2]);
//    }
//    public GenerateNewID.DataCell(Integer IDFrom,Integer IDTo,Integer amount) {
//        this.IDFrom = IDFrom;
//        this.IDTo =   IDTo;
//        this.amount = amount;
//    }
//}